﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;

namespace Web4Comment
{
    public partial class friends : System.Web.UI.Page
    {
        public bool addFriend;
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((String)Session["username"] == null || (String)Session["username"] == "")
            {
                Response.Redirect("./logon.aspx");
            }
            if (Session["addFriend"] != null)
            {
                addFriend = (bool)Session["addFriend"];
            }
            else
            {
                addFriend = false;
            }
            if (!IsPostBack)
            {
                List<UnlessComment.Model.UserTotal> userTotalList = Web4Comment.Service.FriendsService.searchFriends(usernameTextBox.Text == null ? "" : usernameTextBox.Text, (String)Session["username"]);
                nowLabel.Text = "" + 1;
                allLabel.Text = "" + (userTotalList.Count / 5 + (userTotalList.Count % 5 == 0 ? 0 : 1));
                friendsListBind(userTotalList.GetRange(0, userTotalList.Count < 5 ? userTotalList.Count : 5));
                addFriend = false;
                Session["addFriend"] = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (isFriendRadioButton.Checked)
            {
                List<UnlessComment.Model.UserTotal> userTotalList = Web4Comment.Service.FriendsService.searchFriends(usernameTextBox.Text == null ? "" : usernameTextBox.Text, (String)Session["username"]);
                nowLabel.Text = "" + 1;
                allLabel.Text = "" + (userTotalList.Count / 5 + (userTotalList.Count % 5 == 0 ? 0 : 1));
                friendsListBind(userTotalList.GetRange(0, userTotalList.Count < 5 ? userTotalList.Count : 5));
                addFriend = false;
                Session["addFriend"] = false;
            }
            else
            {
                List<UnlessComment.Model.UserTotal> userTotalList = Web4Comment.Service.FriendsService.searchNotFriends(usernameTextBox.Text == null ? "" : usernameTextBox.Text, (String)Session["username"]);
                nowLabel.Text = "" + 1;
                allLabel.Text = "" + (userTotalList.Count / 5 + (userTotalList.Count % 5 == 0 ? 0 : 1));
                friendsListBind(userTotalList.GetRange(0, userTotalList.Count < 5 ? userTotalList.Count : 5));
                addFriend = true;
                Session["addFriend"] = true;
            }
        }



        protected void HandleAdd(object sender, EventArgs e)
        {
            Service.FriendsService.addFriends((String)Session["username"], ((LinkButton)sender).CommandArgument);
            List<UnlessComment.Model.UserTotal> userTotalList = Web4Comment.Service.FriendsService.searchNotFriends(usernameTextBox.Text == null ? "" : usernameTextBox.Text, (String)Session["username"]);
            nowLabel.Text = "" + 1;
            allLabel.Text = "" + (userTotalList.Count / 5 + (userTotalList.Count % 5 == 0 ? 0 : 1));
            friendsListBind(userTotalList.GetRange(0, userTotalList.Count < 5 ? userTotalList.Count : 5));
            addFriend = true;
            Session["addFriend"] = true;
        }

        protected void HandleDelete(object sender, EventArgs e)
        {
            Service.FriendsService.deleteFriends((String)Session["username"], ((LinkButton)sender).CommandArgument);
            List<UnlessComment.Model.UserTotal> userTotalList = Web4Comment.Service.FriendsService.searchFriends(usernameTextBox.Text == null ? "" : usernameTextBox.Text, (String)Session["username"]);
            nowLabel.Text = "" + 1;
            allLabel.Text = "" + (userTotalList.Count / 5 + (userTotalList.Count % 5 == 0 ? 0 : 1));
            friendsListBind(userTotalList.GetRange(0, userTotalList.Count < 5 ? userTotalList.Count : 5));
            addFriend = false;
            Session["addFriend"] = false;
        }

        private void friendsListBind(List<UnlessComment.Model.UserTotal> userTotalList)
        {
            Collection<UnlessComment.Model.UserTotal> userTotals = new Collection<UnlessComment.Model.UserTotal>();

            if (userTotalList == null || userTotalList.Count == 0)
            {
                friendsdl.DataSource = null;
                friendsdl.DataBind();
                return;
            }

            for (int i = 0; i < userTotalList.Count; i++)
            {
                userTotals.Add(userTotalList[i]);
            }

            friendsdl.DataSource = userTotals;
            friendsdl.DataBind();
        }

        protected void preButton_Click(object sender, EventArgs e)
        {
            int now = System.Int32.Parse(nowLabel.Text);
            int all = System.Int32.Parse(allLabel.Text);
            if (now <= 1)
            {
                return;
            }
            now--;
            nowLabel.Text = now.ToString();
            List<UnlessComment.Model.UserTotal> userTotalList = new List<UnlessComment.Model.UserTotal>();
            if (addFriend)
            {
                userTotalList = Web4Comment.Service.FriendsService.searchNotFriends(usernameTextBox.Text == null ? "" : usernameTextBox.Text, (String)Session["username"]);
            }
            else
            {
                userTotalList = Web4Comment.Service.FriendsService.searchFriends(usernameTextBox.Text == null ? "" : usernameTextBox.Text, (String)Session["username"]);
            }
            friendsListBind(userTotalList.GetRange((now - 1) * 5, userTotalList.Count < now * 5 ? userTotalList.Count - (now - 1) * 5 : 5));
        }

        protected void nextButton_Click(object sender, EventArgs e)
        {
            int now = System.Int32.Parse(nowLabel.Text);
            int all = System.Int32.Parse(allLabel.Text);
            if (now >= all)
            {
                return;
            }
            now++;
            nowLabel.Text = now.ToString();
            List<UnlessComment.Model.UserTotal> userTotalList = new List<UnlessComment.Model.UserTotal>();
            if (addFriend)
            {
                userTotalList = Web4Comment.Service.FriendsService.searchNotFriends(usernameTextBox.Text == null ? "" : usernameTextBox.Text, (String)Session["username"]);
            }
            else
            {
                userTotalList = Web4Comment.Service.FriendsService.searchFriends(usernameTextBox.Text == null ? "" : usernameTextBox.Text, (String)Session["username"]);
            }
            friendsListBind(userTotalList.GetRange((now - 1) * 5, userTotalList.Count < now * 5 ? userTotalList.Count - (now - 1) * 5 : 5));
        }

        protected void goButton_Click(object sender, EventArgs e)
        {
            int now = System.Int32.Parse(nowLabel.Text);
            int all = System.Int32.Parse(allLabel.Text);
            int go = System.Int32.Parse(goTextBox.Text);
            if (go > all || go < 1)
            {
                return;
            }
            now = go;
            nowLabel.Text = now.ToString();
            List<UnlessComment.Model.UserTotal> userTotalList = new List<UnlessComment.Model.UserTotal>();
            if (addFriend)
            {
                userTotalList = Web4Comment.Service.FriendsService.searchNotFriends(usernameTextBox.Text == null ? "" : usernameTextBox.Text, (String)Session["username"]);
            }
            else
            {
                userTotalList = Web4Comment.Service.FriendsService.searchFriends(usernameTextBox.Text == null ? "" : usernameTextBox.Text, (String)Session["username"]);
            }
            friendsListBind(userTotalList.GetRange((now - 1) * 5, userTotalList.Count < now * 5 ? userTotalList.Count - (now - 1) * 5 : 5));
        }
    }
}